package com.cgt.web.servlet;

import com.cgt.web.pojo.Classroom;
import com.cgt.web.service.IClassService;
import com.cgt.web.service.impl.ClassServiceImpl;
import com.cgt.web.util.PageInfo;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/classroom")
public class ClassServlet extends HttpServlet {
    private IClassService classService = new ClassServiceImpl();
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("ClassServlet.service");
        String method = req.getParameter("method");
        if(method == null || method.equals("")){
            method = "selectByPage";
        }
        switch (method){
            case "selectByPage":
                selectByPage(req,resp);
                break;
            case "deleteById":
                deleteById(req,resp);
                break;
            case "addInf":
                addInf(req,resp);
                break;
            case "selectById":
                selectById(req,resp);
                break;
            case "updateInf":
                updateInf(req,resp);
                break;
        }
    }

    private void updateInf(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        System.out.println("ClassServlet.updateInf");
        String id = req.getParameter("id");
        String name = req.getParameter("name");
        String address = req.getParameter("address");
        String seatsum = req.getParameter("seatsum");

        Classroom classroom = new Classroom(Integer.parseInt(id),name,address,Integer.parseInt(seatsum));

        classService.updateInf(classroom);

        resp.sendRedirect("/classroom?method=selectByPage");
    }

    private void selectById(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("ClassServlet.selectById");
        String id = req.getParameter("id");

        Classroom classroom = classService.selectById(Integer.parseInt(id));

        req.setAttribute("classroom",classroom);
        req.getRequestDispatcher("class_update.jsp").forward(req,resp);
    }

    private void addInf(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        System.out.println("ClassServlet.addInf");
        String name = req.getParameter("name");
        String address = req.getParameter("address");
        String seatsum = req.getParameter("seatsum");

        Classroom classroom = new Classroom();
        classroom.setName(name);
        classroom.setAddress(address);
        classroom.setSeatsum(Integer.parseInt(seatsum));

        classService.addInf(classroom);

        resp.sendRedirect("/classroom?method=selectByPage");
    }

    private void deleteById(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        System.out.println("ClassServlet.deleteById");
        String id = req.getParameter("id");

        classService.deleteById(Integer.parseInt(id));

        resp.sendRedirect("/classroom?method=selectByPage");
    }

    private void selectByPage(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("ClassServlet.selectByPage");
        String pageNo = req.getParameter("pageNo");
        String pageSize = req.getParameter("pageSize");

        if(pageNo == null || pageNo.equals("")){
            pageNo = "1";
        }
        if(pageSize == null || pageSize.equals("")){
            pageSize = "5";
        }

        PageInfo<Classroom> pageInfo = classService.selectByPage(Integer.parseInt(pageNo),Integer.parseInt(pageSize));

        req.setAttribute("pageInfo",pageInfo);
        req.getRequestDispatcher("class_list.jsp").forward(req,resp);
    }
}
